import path from "path"
import env from "./env"
// 调接口用
import axios from 'axios'

export default {
  // Global page headers: https://go.nuxtjs.dev/config-head
  head: {
    title: 'GetBioJob - Biopharma Jobs & Resume Builder',
    htmlAttrs: {
      lang: 'en',
    },
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1' },
      { hid: 'description', name: 'description', content: 'Get Real Biotech, Biopharma & Life Science Jobs — Smarter and Faster with GetBioJob' },
      { name: 'format-detection', content: 'telephone=no' },
    ],
    link: [{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }],
  },

  // Global CSS: https://go.nuxtjs.dev/config-css
  css: [
    '@/assets/styles/public.scss',
    '@/assets/styles/common.scss',
    'element-ui/lib/theme-chalk/index.css',
    // 直接加载一个 Node.js 模块。（在这里它是一个 Sass 文件）
  ],

  loading: '@/components/AppLoading',

  // Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
  plugins: [
    '@/plugins/element-ui',
    '@/plugins/svg-icon',
    '@/plugins/axios',
  ],

  // Auto import components: https://go.nuxtjs.dev/config-components
  components: true,

  // Modules for dev and build (recommended): https://go.nuxtjs.dev/config-modules
  buildModules: [
    // https://go.nuxtjs.dev/eslint
    //'@nuxtjs/eslint-module',
  ],

  // Modules: https://go.nuxtjs.dev/config-modules
  modules: [
    // https://go.nuxtjs.dev/axios
    '@nuxtjs/axios',
    '@nuxtjs/style-resources',
    '@nuxtjs/sitemap',
    'cookie-universal-nuxt',
  ],

  styleResources: {
    scss: '@/assets/styles/common.scss'
  },

  // Axios module configuration: https://go.nuxtjs.dev/config-axios
  axios: {
    // Workaround to avoid enforcing hard-coded localhost:3000: https://github.com/nuxt-community/axios-module/issues/308
    baseURL: '/',
  },
  // 配置 sitemap
  sitemap: {
    path: '/sitemap.xml',
    hostname: 'http://localhost:8000', // 你的网站域名
    cacheTime: 1000 * 60 * 60 * 24, // 24小时缓存
    gzip: true, // 是否生成 gzip 压缩版本
    generate: false,
    exclude: [
      '/404', // 排除不需要的页面
      '/cmsLogin',
      '/cmsRegister',
      '/indexManage',
      '/indexManage/**'
    ],
    defaults: {
      changefreq: 'daily',
      lastmod: new Date()
    },
    // 对于动态路由，可以使用函数生成
    routes: async () => {
      // 后端接口地址：首页查询文章列表（所有文章无分页）
      const getUrl = `http://127.0.0.1:8080/cms/blog/cms/cmsListAll`
      let res = await axios.get(getUrl)
      let {
        data
      } = res.data

      let routes = [{
        url: "/",
        changefreq: "daily",
        lastmod: new Date()
      }]

      if (data.length !== 0) {
        //拼接
        let arr = data.map(item => ({
          url: "/cmsBlog?id=" + item.id,
          lastmod: item.createTime,
          changefreq: "daily"
        }))
        routes.push(...arr)
      }
      return routes
    }
  },
  server: {
    port: 8000, // default: 3000
    host: '0.0.0.0' // default: localhost只能用于本机访问，如果需要对外访问需要设置0.0.0.0
  },
  router: {
    middleware: 'permission'
  },

  env: {
    ...env[process.env.MODE]
  },

  // Build Configuration: https://go.nuxtjs.dev/config-build
  build: {
    transpile: [/^element-ui/],
    process: null,
    postcss: null,
    extend(config, ctx) {
      // 排除 nuxt 原配置的影响,Nuxt 默认有vue-loader,会处理svg,img等
      // 找到匹配.svg的规则,然后将存放svg文件的目录排除
      const svgRule = config.module.rules.find(rule => rule.test.test('.svg'))
      svgRule.exclude = [path.resolve(__dirname, 'assets/icons/svg')]
      // 添加loader规则
      config.module.rules.push({
        test: /.svg$/, // 匹配.svg
        include: [path.resolve(__dirname, 'assets/icons/svg')], // 将存放svg的目录加入到loader处理目录
        use: [{ loader: 'svg-sprite-loader', options: { symbolId: 'icon-[name]' } }]
      })
    }
  },
}
